package com.itheima.reggie.controller;


import com.itheima.reggie.common.R;
import com.itheima.reggie.domain.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    //查询购物车
    @GetMapping("/list")
    public R list(){

    List<ShoppingCart> list = shoppingCartService.findShopping();

        return R.success(list);
    }

    //添加购物车
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart){

        ShoppingCart cartServiceOne =  shoppingCartService.add(shoppingCart);

        return R.success(cartServiceOne);

    }

    //清空购物车
    @DeleteMapping("/clean")
    public R clean(){
        shoppingCartService.deleteByUserId();

        return R.success(null);
    }

    //购物车数量减1按钮
    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart){

        ShoppingCart sc = shoppingCartService.MinusOne(shoppingCart);

        return R.success(sc);

    }
}
